﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MebApp.Launcher
{
    public class AppProcess
    {
        static Dictionary<string, List<WeakReference<Process>>> processes = new Dictionary<string, List<WeakReference<Process>>>();
        public static void AddProcess(string id, Process process)
        {
            lock (processes)
            {
                if (processes.ContainsKey(id))
                {
                    processes[id].Add(new WeakReference<Process>(process));
                }
                else
                {
                    processes[id] = new List<WeakReference<Process>> { new WeakReference<Process>(process) };
                }
            }
        }

        public static IEnumerable<AppWorkingSet> GetAppWorkingSets(string id)
        {
            lock (processes)
            {
                if (processes.ContainsKey(id))
                {
                    foreach (var item in processes[id])
                    {
                        Process p;
                        if (item.TryGetTarget(out p))
                        {
                            p.Refresh();
                            yield return new AppWorkingSet { ProcessId = p.Id, WorkingSet = p.WorkingSet64 };
                        }
                    }
                }
            }
        }

        public static void RemoveProcess(string id, int processId)
        {
            lock (processes)
            {
                if (processes.ContainsKey(id))
                {
                    foreach (var item in processes[id])
                    {
                        Process p;
                        if (!item.TryGetTarget(out p))
                        {
                            processes[id].Remove(item);
                        }
                        else if (p.Id == processId)
                        {
                            processes[id].Remove(item);
                        }
                    }
                }
            }
        }

        public static int GetProcessCount(string id)
        {
            lock (processes)
            {
                if (processes.ContainsKey(id))
                {
                    return processes[id].Count;
                }
                return 0;
            }
        }

        public static void KillProcesses(string id)
        {
            lock (processes)
            {
                if (processes.ContainsKey(id))
                {
                    foreach (var item in processes[id])
                    {
                        Process process;
                        if (!item.TryGetTarget(out process))
                        {
                            processes[id].Remove(item);
                        }
                        else
                        {
                            process.CloseMainWindow();
                            process.WaitForExit(5000);
                        }
                    }
                }
            }
        }
    }
}
